#include <54func.h>
int main(int argc,char *argv[])
{
    // ./5_aqiang 192.168.187.128 1234
    ARGS_CHECK(argc,3);

    //创建套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,0);

    //初始化服务器地址结构
    struct sockaddr_in serveradd;
    serveradd.sin_family = AF_INET;
    serveradd.sin_port = htons(atoi(argv[2]));
    serveradd.sin_addr.s_addr = inet_addr(argv[1]);
    
    //建立连接
    int ret = connect(sockfd,(struct sockaddr *)&serveradd,sizeof(serveradd));
    ERROR_CHECK(ret,-1,"connect");
    
    fd_set rdset;
    char buf[4096] = {0};
    while(1)
    {
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(sockfd,&rdset);
        select(sockfd+1,&rdset,NULL,NULL,NULL);
        //select返回，说明stdin or sockfd就绪了
        if(FD_ISSET(STDIN_FILENO,&rdset))
        {
            //读oi取stdin的数据
            bzero(buf,sizeof(buf));
            ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
            if(sret == 0)
            {
                printf("azhen is leaver!\n");
                break;
            }
            send(sockfd,buf,strlen(buf),0);//send本质上就是writd，但是send只能王网络李写
        }
        if(FD_ISSET(sockfd,&rdset))
        {
            //读取网络中的数据
            bzero(buf,sizeof(buf));
            ssize_t sret = recv(sockfd,buf,sizeof(buf),0);
            //如果网络的另外一端断开连接，那么我这个从网络读取的这一段会一直读取0
            if(sret == 0)
            {
                printf("azhen已经断开！\n");
                break;//如果网络的另外一端断开连接，那么我这个从网络读取的这一段会一直读取0

            }
            printf("azhen：%s\n",buf);
        }
    }
    return 0;
}
